package com.stgy.lease.web.admin.controller.user;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.stgy.lease.common.result.Result;
import com.stgy.lease.model.domain.UserInfo;
import com.stgy.lease.web.admin.dto.UserInfoSearchDto;
import com.stgy.lease.web.admin.service.UserInfoService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * ClassName:UserController
 * Package:com.stgy.lease.web.admin.controller.user
 * Description:
 *
 * @Author:helei
 * @Create:2024/9/3 - 14:38
 */
@RestController
@RequestMapping("/admin/user")
@Tag(name = "平台用户管理模块")
public class UserController {

    @Autowired
    private UserInfoService userInfoService;

    //http://localhost:5174/admin/user/page?current=1&size=10&phone=15&status=1 GET
    @Operation(summary = "条件查询用户分页列表")
    @GetMapping("/page")
    public Result<Page<UserInfo>> pageItem(UserInfoSearchDto searchDto) {
        return Result.ok(userInfoService.page(searchDto.getPage(UserInfo.class),
                Wrappers.lambdaQuery(UserInfo.class)
                        //当条件不为空 设置status
                        .eq(searchDto.getStatus() != null,
                                UserInfo::getStatus, searchDto.getStatus())
                        .like(!StringUtils.isEmpty(searchDto.getPhone()),
                                UserInfo::getPhone, searchDto.getPhone())
        ));
    }

    //http://localhost:5174/admin/user/updateStatusById?id=1&status=0 POST
    @Operation(summary = "根据id修改账号状态")
    @PostMapping("/updateStatusById")
    public Result<String> updateStatusById(@RequestParam Long id, @RequestParam Integer status) {
        return Result.build(userInfoService.update(Wrappers.lambdaUpdate(UserInfo.class)
                .ge(UserInfo::getId, id)
                .set(UserInfo::getStatus, status)));
    }


}
